ti=prdDt;
locs=unique(ti.location);
hrz=unique(ti.hrzn);
wks=unique(ti.WeekEnding);
mods=unique(ti.model);
wksall=ti.WeekEnding(:);
hrzall=ti.hrzn(:);
locsAll=prdDtNw.location(:);
errAll=ti.Error(:);
modAll=ti.model(:);

wins=zeros(numel(mods),numel(wks),numel(locs),numel(hrz));
cmps=wins;

for i=1:numel(wks)
    for l=1:numel(locs)
        for j=1:numel(hrz)
            inx=wksall==wks(i) & hrzall==hrz(j) & matches(locsAll,locs{l});
            if sum(inx)>1
                errs=errAll(inx);
                mdl=modAll(inx);
                [~,sinx]=sort(errs,'descend');
                
                for k=1:numel(errs)
                    minx=find(matches(mods,mdl{k}));
                    wins(minx,i,l,j)=find(sinx==k)-1;
                    cmps(minx,i,l,j)=numel(errs)-1;
                end
            end
        end
    end
end

winf=wins./cmps;
[xx,yy]=sort(mean(winf(:,:,:,7:end),[2 3 4],'omitnan'));
zz=sum(squeeze(cmps(yy,:,:,:)>0),[2 3 4]);
[xx,zz,yy]
sf1=4;sf2=6;
inxCm=squeeze(cmps(yy(end-sf1),:,:,:)>0) & squeeze(cmps(yy(end-sf2),:,:,:)>0);
sum(inxCm,'all')
mean(squeeze(wins(yy(end-sf1),inxCm))>=squeeze(wins(yy(end-sf2),inxCm)),'all')

i1=2; i2=61; hrzn=9:hrz(end);
inxCm=squeeze(cmps(i1,:,:,hrzn)>0) & squeeze(cmps(i2,:,:,hrzn)>0);
sum(inxCm,'all')
mean(squeeze(wins(i1,:,:,hrzn))>=squeeze(wins(i2,:,:,hrzn)),'all')

mean(squeeze(winf(yy(end-1),inxCm)),'all')
save('racesV8','wins','cmps','winf');